package org.springblade.modules.admin.task.feishu;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.xxl.job.core.context.XxlJobHelper;
import com.xxl.job.core.handler.annotation.XxlJob;
import lombok.extern.slf4j.Slf4j;
import org.springblade.common.enums.FeiShuDocumentEnum;
import org.springblade.modules.admin.dto.feishu.*;
import org.springblade.modules.admin.service.ElevatorService;
import org.springblade.modules.admin.service.feishu.IFeiShuDocumentService;
import org.springblade.modules.admin.vo.feishu.GalaxyElevatorNumVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.*;

/**
 * 电梯识别码 飞书云文档 Job
 * @author: Sharry
 * @createTime: 2024-01-25 15:17
 * @version: Version-1.0
 */
@Component
@Slf4j
public class GalaxyElevatorNumsJob {

	@Autowired
	private ElevatorService elevatorService;

	@Autowired
	private IFeiShuDocumentService feiShuDocumentService;

	@XxlJob("galaxyElevatorNumsJob")
	public void galaxyElevatorNumsJob() {
		XxlJobHelper.handleSuccess("定时任务更新银河租户电梯识别码信息-[执行]开始");
		try{
			// 0. 更新流程：先列出全部记录，获取当前全部record_id 集合。现阶段先删除后新增，则完成更新
			String result = "";
			// 1. 数据准备：目标文件夹Token、文档ID; 需要更新的数据列表
			// 1.1 目标文件夹Token、文档ID
			String appToken = FeiShuDocumentEnum.GALAXY_ELEVATOR_NUMS.getAppToken();
			String tableId = FeiShuDocumentEnum.GALAXY_ELEVATOR_NUMS.getTableId();
			List<GalaxyElevatorNumVO> galaxtElevatorNumVOList = elevatorService.getGalaxyElevatorNumVOs();
			// 1.2 数据封装
			FeiShuCreateBatchDTO feiShuCreateBatchDTO = new FeiShuCreateBatchDTO();
			List<UpdateMaintenancePersonnelDTO> records = new ArrayList<>();
			for (GalaxyElevatorNumVO u : galaxtElevatorNumVOList) {
				UpdateMaintenancePersonnelDTO element = new UpdateMaintenancePersonnelDTO();
				Map<String, Object> elementMap = new HashMap<>(10);
				elementMap.put("梯无忧-电梯快速识别码", u.getElevatorNum());
				elementMap.put("电梯位置", u.getElevatorAddress());
				elementMap.put("项目名称", u.getBuildingName());
				element.setFields(elementMap);
				records.add(element);
			}
			// records 去重
			Set<UpdateMaintenancePersonnelDTO> set = new HashSet<>(records);
			// 清空 records, 将 set 中的数据添加到 records 中
			records.clear();
			records.addAll(set);
			feiShuCreateBatchDTO.setRecords(records);
			result = feiShuDocumentService.commandCompareUpdateBatch(appToken, tableId,feiShuCreateBatchDTO,"梯无忧-电梯快速识别码");
			// 2. 发送消息, 通知到群
			List<JSONObject> targetGroupObjects = feiShuDocumentService.commandQueryGroupsBatch();
			List<String> chatIds = new ArrayList<>();
			targetGroupObjects.forEach(t -> chatIds.add(t.getString("chat_id")));
			// 3. 发送消息
			String finalResult = result;
			chatIds.forEach(c -> {
				// 组装数据
				JSONObject bodyJson = new JSONObject();
				bodyJson.put("receive_id", c);
				bodyJson.put("msg_type", "text");
				HashMap<String, String> contentMap = new HashMap<>(2);
				String message = "定时任务更新银河租户电梯识别码信息，结果：" + finalResult;
				contentMap.put("text", message);
				// 将 contentMap 转换成 JSON 格式
				bodyJson.put("content", JSON.toJSONString(contentMap));
				feiShuDocumentService.commandSendGroupMessage(bodyJson.toJSONString());
			});
			XxlJobHelper.handleSuccess("定时任务更新银河租户电梯识别码信息-[执行]成功");
		}catch (Exception e){
			XxlJobHelper.handleSuccess("定时任务更新银河租户电梯识别码信息-[执行]失败"+e);
		}

	}

}
